import datetime
from contextlib import asynccontextmanager

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger
from fastapi import FastAPI


scheduler = AsyncIOScheduler()

YMDHM_FORMATTER = f"%Y-%m-%d %H:%M"
YMD_FORMATTER = f"%Y-%m-%d"
YM_FORMATTER = f"%Y-%m"


# @asynccontextmanager
# async def add_auth_to_swagger(app: FastAPI):
#     if app.openapi_schema:
#         yield app.openapi_schema
#
#     openapi_schema = app.openapi()
#     # 添加 BearerAuth 安全机制到 OpenAPI 中
#     openapi_schema["components"]["securitySchemes"] = {
#         "BearerAuth": {
#             "type": "http",
#             "scheme": "bearer",
#             "bearerFormat": "JWT"
#         }
#     }
#     # 默认应用 BearerAuth 认证到每个 API 路由
#     openapi_schema["security"] = [{"BearerAuth": []}]
#     app.openapi_schema = openapi_schema
#     yield


# 主生命周期管理器
@asynccontextmanager
async def lifespan(app: FastAPI):
    scheduler.start()
    # # # 每天备份一下数据表
    # scheduler.add_job(export_database, trigger=CronTrigger(hour=0, minute=0))
    # # 每天0点检测修改交割状态
    # scheduler.add_job(modify_handover_status, trigger=CronTrigger(hour=0, minute=1))
    # # 修改逾期状态
    # scheduler.add_job(modify_billing_contract_status, trigger=CronTrigger(hour=0, minute=1))
    # # 修改账单违约金
    # scheduler.add_job(modify_bill_late_fee_value, trigger=CronTrigger(hour=0, minute=2))
    # # 自动处理过期合同
    # scheduler.add_job(handle_expire_contract, trigger=CronTrigger(hour=0, minute=1))
    # # 自动处理合同签约状态
    # scheduler.add_job(handle_contract_sign_status, trigger=CronTrigger(hour=0, minute=1))
    # # 将超过1小时未处理的合同进行作废处理
    # scheduler.add_job(deal_ttl_contract, trigger=IntervalTrigger(minutes=10))
    # # 未处理的预定进行删除处理-如果1小时之内，没有确认，也没有交定金
    # scheduler.add_job(deal_ttl_agreement, trigger=IntervalTrigger(minutes=10))
    # # 将超时的预定订单改为过期未签
    # scheduler.add_job(deal_expire_unsign, trigger=CronTrigger(hour=0, minute=10, timezone="Asia/Shanghai"))
    # # 更新门锁信息，每小时执行
    # # scheduler.add_job(modify_electronic_lock_info, trigger=IntervalTrigger(hours=1))
    # # 每天8点检测，长时间没开门告警
    # # scheduler.add_job(open_door_alert, trigger=CronTrigger(hour=8, minute=0))
    # #  查询租客账单未缴 - 提前一天预警
    # scheduler.add_job(modify_billing_alarm, trigger=CronTrigger(hour=0, minute=1, timezone="Asia/Shanghai"))
    # # 租客24小时未签约 - 提前3小时预警 - 需求删除
    # # scheduler.add_job(modify_sign_alarm, trigger=IntervalTrigger(hours=1))
    # # 门锁信号差、低电、离线预警、7天重复一次
    # # scheduler.add_job(modify_lock_info_alarm, trigger=IntervalTrigger(days=7, hours=0, minutes=0))
    # # 门锁超过一个月未使用，安全预警，7天重复一次
    # # scheduler.add_job(modify_lock_unuse_alarm, trigger=IntervalTrigger(days=7, hours=0, minutes=0))
    # # 门锁开门记录采集
    # # scheduler.add_job(collect_lock_record, trigger=IntervalTrigger(days=1, hours=1, minutes=0))
    # # scheduler.add_job(collect_lock_record, trigger=IntervalTrigger(seconds=60))
    # # 更新合同最新使用时间
    # # scheduler.add_job(modify_electronic_lock_using_record, trigger=CronTrigger(hour=0, minute=1, timezone="Asia/Shanghai"))
    #
    # # 生成薪水,每天1点运行一次
    # scheduler.add_job(generate_salary, trigger=CronTrigger(hour=1, minute=0))
    # # 续租合同自动划押金的各逻辑处理
    # scheduler.add_job(deal_renewal_contract_status, trigger=CronTrigger(hour=0, minute=0))
    # # scheduler.add_job(deal_renewal_contract_status, trigger=IntervalTrigger(seconds=3))
    # # 修改优惠配置状态,每天1点
    # scheduler.add_job(modify_discount_status, trigger=CronTrigger(hour=1, minute=0))
    # # 每天0点检测商业房源状态
    # scheduler.add_job(modify_biz_asset_status, trigger=CronTrigger(hour=0, minute=0))
    # # 每天0点检测商业账单状态
    # scheduler.add_job(modify_biz_billing_status, trigger=CronTrigger(hour=0, minute=0))

    # async with add_auth_to_swagger(app):
    #     yield  # 进入FastAPI生命周期
    scheduler.shutdown()
